<template>
  <div class="type-nav">
    <div class="container-nav">
      <div @mouseleave="leaveShow" @mouseenter="enterShow">
        <h2 class="all">全部科室</h2>
        <!-- 过渡动画 -->
        <transition name="sort">
          <div class="sort" v-show="show">
            <div class="all-sort-list2" @click="goSearch">
              <div class="item" v-for="(c1, index) in categoryList" :key="c1.pid">
                <h3 :class="{ cur: currentIndex === index }" @mouseenter="changeIndex(index)">
                  <a>{{ c1.departmentName }}</a>
                </h3>
                <div v-show="showDis" @mouseleave="leaveShowDis" class="item-list clearfix" :style="{ display: currentIndex === index ? 'block' : 'none' }">
                  <div class="subitem">
                    <!--<div v-for="(c2,index) in c1.diseaseList">-->
                    <!--  {{c2}}-->
                    <!--</div>-->
                    <dl class="fore"  v-for="(c2,index) in c1.diseaseList" :key="c2.pid" >
                      <h3 class="disease-h3" :class="{ disease: currentDiseaseIndex === index }"  @mouseenter="changeDiseaseIndex(index)">
                        <a :data-categoryName="c2">{{ c2 }}</a>
                      </h3>
                      <!--<dt :class="{ disease: currentDiseaseIndex === index }" @mouseenter="changeDiseaseIndex(index)">-->
                      <!--  <a :data-categoryName="c2">{{ c2 }}</a>-->
                      <!--</dt>-->
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>
// import {mapState} from 'vuex'
//引入lodash
import throttle from 'lodash/throttle'
import { Loading } from 'element-ui';
export default {
  name: "VideoNav",
  mounted() {
    this.$store.dispatch('categoryList').then((res) => {
      this.categoryList = res.departments
      //缓存
      let departStr = JSON.stringify(res.departments)
      localStorage.setItem('departments', departStr)
    })
    if (this.$route.path !== "/") {
      this.show = false
    }
  },
  data() {
    return {
      //存储用户鼠标移上哪一个一级分类
      currentIndex: -1,
      show: true,
      categoryList: [],
      currentDiseaseIndex: -1,
      showDis: true

    }
  },
  methods: {
    changeIndex: throttle(function (index) {
      this.currentIndex = index
    }, 50),
    changeDiseaseIndex: throttle(function (index) {
      this.currentDiseaseIndex = index
    }, 50),
    //进行路由跳转的方法
    goSearch(event) {
      //event.target:获取到的是出发事件的元素(div、h3、a、em、dt、dl)
      let node = event.target;
      //给a标签添加自定义属性data-categoryName,全部的字标签当中只有a标签带有自定义属性
      let {
        categoryname,
      } = node.dataset;
      //第二个问题解决了：点击的到底是不是a标签（只要这个标签身上带有categoryname）一定是a标签
      //当前这个if语句：一定是a标签才会进入
      if (categoryname) {
        //判断：如果路由跳转的时候，带有params参数，捎带传递过去
        if (this.$route.params) {
          //路由跳转
          let loading = Loading.service({ fullscreen: true });
          this.$router.push(this.$route.path+"?disease="+categoryname);
          location.reload()
          loading.close()


        }
      }
    },
    //当鼠标移入的时候，让商品分类列表进行展示
    enterShow() {
      if (this.$route.path !== "/home") {
        this.show = true;
      }
    },
    //当鼠标离开的时候，让商品分类列表进行隐藏
    leaveShow() {
      this.currentIndex = -1;
      //判断如果是Search路由组件的时候才会执行
      if (this.$route.path !== "/home") {
        this.show = false;
      }
    },
    leaveShowDis() {
      this.currentDiseaseIndex = -1;
      //判断如果是Search路由组件的时候才会执行
      if (this.$route.path !== "/home") {
        this.showDis = false;
      }
    }
  }
}
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #419dbf;

  .container-nav {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #419dbf;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      cursor: pointer;

    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 580px;
      background: #fafafa;
      z-index: 999;
      //overflow: hidden;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 600;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #797979;
            }

            &.cur {
              background: rgba(53, 102, 177, 0.21);
              cursor: pointer;
              //font-size: 20px;
              //color: #a34d4d;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 190px;
            min-height: 417px;
            background: #fafafa;
            left: 210px;
            border-left: 1px solid #e8e8e8;
            top: -7px;
            z-index: 9999 !important;
            //.disease-h3{
            //  position: relative;
            //  left: -20px;
            //  margin-left: 0px;
            //}
            a {
              color: #797979;
            }
            .disease{
              width: 150px;
              background: rgba(53, 102, 177, 0.21);
              cursor: pointer;
              padding-left: 20px;
            }
            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  position: relative;
                  left: -8px;
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 200px;
                  line-height: 22px;
                  text-align: left;
                  padding: 3px 6px 0 20px;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }
        }
      }
    }

    //过渡动画的样式
    //过渡动画开始状态（进入）
    .sort-enter {
      height: 0px;
      overflow: hidden;
    }

    // 过渡动画结束状态（进入）
    .sort-enter-to {
      height: 580px;
      overflow: hidden;
    }

    // 定义动画时间、速率
    .sort-enter-active {
      transition: all 0.6s linear;
    }
  }
}
</style>
